package com.bilibili.xzz.controller;

import com.bilibili.xzz.entity.MessageBoard;
import com.bilibili.xzz.service.MessageBoardService;
import com.bilibili.xzz.utils.Result;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/messageboard")
public class MessageBoardController {
    @Resource
    MessageBoardService messageBoardService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/info")
    public Result MessageBroad() {
        try {
            List<MessageBoard> messageBoardList = messageBoardService.getAllMessageBoard();
            return Result.ok().data("info", messageBoardList);
        } catch (Exception e) {
            return Result.error().data("msg", "获取信息失败").data("err", e.toString());
        }
    }

    @PostMapping("/info")
    public Result insertMessageBoard(@RequestBody MessageBoard messageBoard) {
        int num = messageBoardService.insertMessageBoard(messageBoard);
        if (num == 1) {
            return Result.ok().data("info", "插入成功");
        } else {
            return Result.error().data("info", "插入失败");
        }
    }

    @PostMapping("agree")
    public Result updateMessageBoard(@RequestParam Integer agree, @RequestParam String id) {

        int num = messageBoardService.updateMessageBoard(id, agree);
        if (num == 1) {
            return Result.ok().data("info", "更新成功");
        } else {
            return Result.error().data("info", "更新失败");
        }

    }

    @PostMapping("disagree")
    public Result disagreeMessageBoard(@RequestParam Integer disagree, @RequestParam String id) {

        int num = messageBoardService.disagreeMessageBoard(id, disagree);
        if (num == 1) {
            return Result.ok().data("info", "更新成功");
        } else {
            return Result.error().data("info", "更新失败");
        }


    }
//    @PostMapping("/agree")
//    public Result agree(){
//        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
//        operations.increment("agree");
//        return Result.ok().data("agree_success",operations.get("agree"));
//    }
//
//    @PostMapping("/disagree")
//    public Result disagree(){
//        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
//        operations.increment("disagree");
//        return Result.ok().data("disagree_success",operations.get("disagree"));
//    }

}
